<?php

/**
 * SiteController is the default controller to handle user requests.
 */
class SitefbController extends FBController
{
	//public $sidebar = 'RecentComments';
	public $sidebaritems = array('title' => 'letzte Informationen ...');
public function actions(){
		return array(
			'captcha'=>array(
				'class'=>'MathCaptchaAction',
				'backColor'=>0xFFFFFF,
				'minLength' => 1,
				'maxLength' => 10,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * Index action is the default action in a controller.
	 */
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}
	/**
	 * Displays the default page
	 */
	public function actionIndex()
	{
	      	$model = Yii::app()->db->createCommand()
    		->select()
    		->from('{{profil}}')
    		->where('uid="'.$this->FBUser.'"')
    		->queryRow();
    	// set default values of none are available
    	if ($model == null){
    		$model = new Profil;
    		$model->uid = $this->FBUser;
    		$model->lon = 48.8657844;
    		$model->lat = 2.3073141;
    		$model->town = 'Paris';
    		$model->street = 'Champs-Elysees';
    		$model->distance = 25;
    		$model->save();

    	}
    	
		$this->render('index', array(
				'profile'=>$this->FBUser_profile, 
				'loggedin'=> $this->logged_in, 
				'FBReturn' => $this->FBReturn,
				));
	}
	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form

		$this->render('login',array('model'=>$model));
	}
		/**
	 * Displays the site page
	 */
	public function actionMap()
	{
		Yii::app()->clientScript->registerMetaTag('PEC Consulting, St.Gallen. Sitemap zum Profil von Paul P. Eberle und zu den Leistungen in den Bereichen Test Data Management, Test Services und Consulting.');
		$page=new Page();
		$model=$page->getPageStructure(6);
		$this->render('map', array(
			'model'=>$model,
			));
	}
	/**
	 * Displays the SITE View
	 */
	public function actionView($id)
	{ 
		//require dirname(__FILE__).'/../ppe/common/fb/Facebook.php';

		// Create our Application instance (replace this with your appId and secret).
		$facebook = new Facebook(array(
  				'appId'  => '424168224273806',
  				'secret' => 'a5acf3c1936efc02a39d03e5e91b9fbf',
		));

		// Get User ID
		$user = $facebook->getUser();
		if($user){
	 $accesstoken=$facebook->getAccessToken();
	 //echo 'Token: '.$accesstoken;
      try {
      /*
              $fql = 	'SELECT post_id, actor_id, app_id, message, attribution, type  FROM stream WHERE '. 
              			'source_id = 424168224273806 '; //and link = "'.$_SERVER['REQUEST_URI'].'"';
        $ret_obj = $facebook->api(array(
                                   'method' => 'fql.query',
                                   'query' => $fql,
                                 ));
                                 */
$ret_obj = $facebook->api('/424168224273806/tagged','GET');
      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        /* just ask the user to login again here.
        $login_url = $facebook->getLoginUrl( array(
                       'scope' => 'publish_stream'
                       )); 
                       
        echo 'Please <a href="' . $login_url . '">login.</a>';
*/
		print_r($e);
        echo $e->getType();
        echo $e->getMessage();
      }

		}
		if($id == 1) $this->render('gainground', array('fbcomment' => $ret_obj));
		elseif($id == 2) $this->render('playground');
		elseif($id == 3) $this->render('background');
		elseif($id == 10) $this->render('csoudit');		
		else $this->render('index');
	}
	
	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}